package net.tv90.bfmccoreqqbot.util.bf4;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.tv90.bfmccoreqqbot.util.HttpUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BF4Util {

    public static Map<String, UserAccountBasicInfo> bf4PersonalIdCache = new ConcurrentHashMap<>();


    public static UserAccountBasicInfo getBF4UserInfo(String username) {
        if(bf4PersonalIdCache.containsKey(username)) {
            return bf4PersonalIdCache.get(username);
        }
        JSONObject response = HttpUtil.httpPost("https://battlelog.battlefield.com/bf4/search/query/",
                "query=" + username
        );
        try {
            if (response != null) {
                JSONArray json = response.getJSONArray("data");
                String personalId = json.getJSONObject(0).getString("personaId");
                String userId = json.getJSONObject(0).getString("userId");
                if(personalId != null && userId != null) {
                    UserAccountBasicInfo info = new UserAccountBasicInfo(personalId, userId);
                    bf4PersonalIdCache.put(username, info);
                    return info;
                }
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    public static UserStats getBF4UserStats(String username) {
        try {
            UserAccountBasicInfo info = getBF4UserInfo(username);
            if(info == null) {
                return null;
            }
            String s = "https://battlelog.battlefield.com/bf4/warsawdetailedstatspopulate/"+info.personalId+"/1/";
            JSONObject response = HttpUtil.httpGet(s);
            JSONObject json = response.getJSONObject("data").getJSONObject("generalStats");
            UserStats userStats = UserStats.builder()
                    .kills(json.getString("kills"))
                    .deaths(json.getString("deaths"))
                    .kd(json.getString("kdRatio"))
                    .killAssists(json.getString("killAssists"))
                    .headshots(json.getString("headshots"))
                    .shotsFired(json.getString("shotsFired"))
                    .timePlayed(json.getString("timePlayed"))
                    .longestKillStreak(json.getString("killStreakBonus"))
                    .numWins(json.getString("numWins"))
                    .numLosses(json.getString("numLosses"))
                    .rank(json.getString("rank"))
                    .kpm(json.getString("killsPerMinute"))
                    .score(json.getString("score"))
                    .revives(json.getString("revives"))
                    .skill(json.getString("skill"))
                    .shotsHit(json.getString("shotsHit"))
                    .longestHeadShots(json.getString("longestHeadshot") + "m")
                    .vehicleDestroyed(json.getString("vehiclesDestroyed"))
                    .spm(json.getString("scorePerMinute"))
                    .build();

            //acc
            float acc = Float.parseFloat(json.getString("accuracy"));
            userStats.setAcc(String.format("%.3f", acc) + "%");
            //wr
            float numWins = Float.parseFloat(userStats.getNumWins());
            float numLosses = Float.parseFloat(userStats.getNumLosses());
            String winRate = String.format("%.3f",
                    numWins / (numWins + numLosses));
            userStats.setWinRate(winRate);
            //hskr
            float headShots = Float.parseFloat(userStats.getHeadshots());
            float kills = Float.parseFloat(userStats.getKills());
            String hskr = String.format("%.3f",
                    headShots * 100 / kills) + "%";
            userStats.setHeadshotsRatio(hskr);

            return userStats;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}